package com.kay.bboss.elasticsearch.crud;

import org.frameworkset.elasticsearch.ElasticSearchException;
import org.frameworkset.elasticsearch.ElasticSearchHelper;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.frameworkset.elasticsearch.entity.ESDatas;

import java.sql.Date;
import java.text.ParseException;

/**
 * Created by Kay on 2018/10/28
 */
public class BasicInfoCRUD {
    //创建索引
    public void testCreateIndice(){
        //创建加载配置文件的客户端工具 单实例多线程安全
        ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/demo.xml");
        try{
            // 删除 mapping
            clientUtil.dropIndice("client_info");
        }catch (ElasticSearchException e){
            e.printStackTrace();
        }

        //创建mapping
        clientUtil.createIndiceMapping("client_info","createInfoIndice");
    }

    public void addDocument(BasicInfo basicInfo)
    {
        //创建创建/修改/获取/删除文档的客户端对象，单实例多线程安全
        ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
        //构建一个对象，日期类型，字符串类型属性演示


        //向固定index demo添加或者修改文档,如果demoId已经存在做修改操作，否则做添加文档操作，返回处理结果
        String response = clientUtil.addDocument("client_info",//索引表
                "client_info",//索引类型
                basicInfo);
    }

    //
    public ESDatas<BasicInfo> searchAll(){
        //创建创建/修改/获取/删除文档的客户端对象，单实例多线程安全
        ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/demo.xml");
        //构建一个对象，日期类型，字符串类型属性演示
        return clientUtil.searchList("client_info/_search","searchAllBasicInfo",BasicInfo.class);
    }
}
